#include "numeric_conversion.h"


QByteArray NumericConversion::IntToArray(unsigned int v)
{
    int size = sizeof (unsigned int);

    QByteArray a;
    a.resize(size);

    for(int i = 0; i < size; i++){
        a[i] = static_cast<char>( (v >> ( (size - i -1) * 8) ) & 0xff );
    }

    return a;
}

QByteArray NumericConversion::ShortToArray(unsigned short v)
{
    int size = sizeof (unsigned short);

    QByteArray a;
    a.resize(size);

    for(int i = 0; i < size; i++){
        a[i] = static_cast<char>( (v >> ( (size - i -1) * 8) ) & 0xff );
    }

    return a;
}

unsigned int NumericConversion::ArrayToInt(const QByteArray in)
{
    if (in.size()==0)
    {
        return -1;
    }
    else
    {
        bool ok = false;
        int ret = in.toHex().toUInt(&ok, 16);
        if(ok)
        {
            return ret;
        }
        return -1;
    }
}
